import 'package:flutter/material.dart';
import 'package:get/get.dart';

import 'package:speed_random/pages/home/rand_com/com_results/item_results.dart';

import 'package:speed_random/widgets/not_data.dart';

import 'logic.dart';
import 'state.dart';

class ComResultsPage extends StatelessWidget {
  ComResultsPage({Key? key}) : super(key: key);

  final ComResultsLogic logic = Get.put(ComResultsLogic());
  final ComResultsState state = Get.find<ComResultsLogic>().state;

  // 显示确认对话框
  Future<void> _showClearConfirmationDialog(BuildContext context) async {
    return showDialog<void>(
      context: context,
      barrierDismissible: false, // 用户不能通过点击对话框外部关闭对话框
      builder: (BuildContext context) {
        return AlertDialog(
          title:  Text('确认清空'.tr),
          content:  SingleChildScrollView(
            child: ListBody(
              children: <Widget>[
                Text('确定要清空所有随机数历史记录吗？'.tr),
              ],
            ),
          ),
          actions: <Widget>[
            TextButton(
              child:  Text('取消'.tr),
              onPressed: () {
                Navigator.of(context).pop();
              },
            ),
            TextButton(
              child:  Text('确定'.tr),
              onPressed: () {
                logic.clearHistory();
                Navigator.of(context).pop();
              },
            ),
          ],
        );
      },
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title:  Obx(()=>Text('历史记录'.tr+'(${state.historyNumbers.value.length})')),
       // backgroundColor: AppStyles.colorAppBarbg,
        actions: [
          IconButton(
            icon: const Icon(Icons.delete),
            onPressed: () {
              _showClearConfirmationDialog(context);
            },
          ),
        ],
      ),
      body: Obx(() => Container(
       // color: AppStyles.colorPagebg,  // 确保body也有背景色
        child: state.historyNumbers.value.length==0? notData(): ListView.builder(
          itemCount: state.historyNumbers.value.length,
          itemBuilder: (context, index) {
            final indext = state.historyNumbers.value.length - 1 - index;
            final numbers = state.historyNumbers.value[indext];



            return ItemResults(logic: logic, state: state, numbers: numbers,bgColor: state.randComArgs.value.colorPagebg,);
          },
        ),
      )),
    );
  }
}
